package com.aier.service.form.impl;


import com.aier.bean.common.FieldDescription;
import com.aier.service.form.FormService;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.*;

@Service
public class FormServiceImpl implements FormService {
    @Override
    public List<Map<String, List<String>>> getObjectFieldAndType(String classPath) throws ClassNotFoundException {
        Class<?> clazz = Class.forName(classPath);
        Field[] fields = clazz.getFields();
        Field[] declaredFields = clazz.getDeclaredFields();
        List<Map<String, List<String>>> fieldMapList = new ArrayList<>();
        for (Field field : fields) {
            FieldDescription annotation = field.getAnnotation(FieldDescription.class);
            Map<String, List<String>> map = new HashMap<>();
            List<String> typeAndDesc = new ArrayList<>();
            typeAndDesc.add(field.getType().getSimpleName());
            typeAndDesc.add(annotation.value());
            map.put(field.getName(),typeAndDesc);
            fieldMapList.add(map);
        }
        Arrays.stream(declaredFields).forEach(field->{
            FieldDescription annotation = field.getAnnotation(FieldDescription.class);
            Map<String, List<String>> map = new HashMap<>();
            List<String> typeAndDesc = new ArrayList<>();
            typeAndDesc.add(field.getType().getSimpleName());
            typeAndDesc.add(annotation.value());
            map.put(field.getName(),typeAndDesc);
            fieldMapList.add(map);
        });
        return fieldMapList;
    }
}
